<template>
  <div id="home">
    <div class="home-nav">
      <nav-bar>
        <div slot="center">购物街</div>
      </nav-bar>
    </div>
    <tab-control
      class="tab-control"
      :titles="titles"
      @itemClick="tabClick"
      ref="topTabControl"
      v-show="isTabFixed"
    ></tab-control>
    <scroll
      :probe-type="3"
      :pull-up-load="true"
      @scrollY="contentScroll"
      @pullingUp="loadMore"
      ref="scroll"
    >
      <swiper :banners="banners" @swiperImgLoad="swiperImgLoad"></swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <div class="tab-control-wrap">
        <tab-control
          class="tab-control"
          :titles="titles"
          @itemClick="tabClick"
          ref="tabControl"
          v-show="!isTabFixed"
        ></tab-control>
      </div>
      <goods-list :goods="showGoods"></goods-list>
    </scroll>
    <!-- 当我们需要监听一个组件的原生事件时，必须给对应的事件加上.native才能进行监听 -->
    <back-top v-if="backTopGlog" @click.native="backTop"></back-top>
  </div>
</template>
<script>
import bus from "utils/homeImgBus/bus";
import { getHomeMultidata, getHomeGoogs } from "api/home";

import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";

import NavBar from "components/common/navBar/NavBar";
import Swiper from "components/common/swiper/Swiper";
import Scroll from "components/common/scroll/Scroll";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";

import { itemListenerMixin, backTopMixin, tabControlMixin } from "@/common/mixin.js";

import { POP, SELL, NEW } from "@/common/const";
export default {
  name: "home",
  components: {
    RecommendView,
    FeatureView,
    NavBar,
    Swiper,
    TabControl,
    GoodsList,
    Scroll,
  },
  mixins: [itemListenerMixin, backTopMixin, tabControlMixin],
  data() {
    return {
      banners: [],
      recommends: [],
      titles: ["流行", "新款", "精选"],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      // currentType: "pop",
      // backTopGlog: false,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  mounted() {
    let that = this;
    // 1.请求多个数据
    this.getHomeMultidata();
    // 2.获取首页商品
    this.getHomeGoogs("pop");
    this.getHomeGoogs("new");
    this.getHomeGoogs("sell");
    // 3.监听图片加载完成,重新计算一下scrollerHeight(这块是公共的，我们将这里抽离出来放到了mixin.js中)
    /**
     *
    const refresh = debounce(this.$refs.scroll.refresh, 500);
    this.itemImgListener = function () {
      // that.$refs.scroll.refresh();
      refresh();
    }
    bus.$on("itemImgLoad", this.itemImgListener);
    */
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
    this.$refs.scroll.refresh();
  },
  deactivated() {
    // 1.保存Y值
    this.saveY = this.$refs.scroll.bScroll.y;
    // 2.取消全局事件的监听
    bus.$off("itemImgLoad", this.itemImgListener);
  },
  methods: {
    /**
     * 事件监听(将这里抽离到mixin.js中)
     */
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = POP;
          break;
        case 1:
          this.currentType = NEW;
          break;
        case 2:
          this.currentType = SELL;
          break;
        default:
          this.currentType = POP;
          break;
      }
      // 点击之后对tabControl组件中的currentIndex重新赋值
      if (this.$refs.topTabControl !== undefined) {
        this.$refs.topTabControl.currentIndex = index;
        this.$refs.tabControl.currentIndex = index;
      }
    },
    // backTop() { // 抽离到了mixins中
    //   /**
    //    * this.$refs.scroll拿到子组件对象
    //    */
    //   this.$refs.scroll.scrollTo(0, 0, 500);
    // },
    contentScroll(y) {
      // console.log(y);
      // 1.判断BackTop是否显示
      if (y >= 1000) {
        this.backTopGlog = true;
      } else {
        this.backTopGlog = false;
      }
      // 2.决定tabControl是否吸顶(position:fixed)
      this.isTabFixed = y >= this.tabOffsetTop;

    },
    loadMore() {
      this.getHomeGoogs(this.currentType);
      // 异步加载完，重新计算一下高度
      this.$refs.scroll.refresh();
    },
    // 4.获取tabControl的offsetTop
    /**
     *所有的组件都有一个属性$el:用于获取组件中的元素
     */
    swiperImgLoad() {
      this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop;
      // console.log(this.tabOffsetTop);
    },

    /**
     * 网络请求相关
     */
    getHomeMultidata() {
      getHomeMultidata()
        .then((res) => {
          this.banners = res.data.data.banner.list;
          this.recommends = res.data.data.recommend.list;

        })
        .catch((err) => {
          console.log("home请求多个数据：", err);
        });
    },
    // 获取更多商品
    getHomeGoogs(type) {
      let page = this.goods[type].page + 1;
      getHomeGoogs(type, page)
        .then((res) => {
          this.goods[type].list.push(...res.data.data.list);
          this.goods[type].page += 1;
          // 多次使用上拉加载更多
          this.$refs.scroll.finishPullUp();
        })
        .catch((err) => {
          console.log("home商品：", err);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
#home {
  // padding: 44px 0 50px;
  position: relative;
  height: 100vh; // 注意这个的含义：表示相对视口高度（Viewport Height），1vh = 1% * 视口高度。

  .home-nav {
    background: var(--color-high-text);
    color: #fff;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 10;
  }
  .tab-control-wrap {
    height: 44px;
  }
  .tab-control {
    position: sticky;
    top: 44px;
    left: 0;
    right: 0;
    z-index: 10;
  }
  // 这里受better-scroll的影响并不起作用，可将tab-control复制一份到scroll外面进行，将这两个是否显示进行判读
  .fixed {
    position: fixed;
    left: 0;
    right: 0;
    top: 44px;
  }
}
</style>
